<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>本地存储 </title>
</head>

<body>
    <h2>localStorage</h2>
    <button onclick="saveDate()">点我保存一个数据</button>
    <button onclick="readDate()">点我读取一个数据</button>
    <button onclick="deletDate()">点我删除一个数据</button>
    <button onclick="deletAllDate()">清空</button>


    <script type="text/javascript">

        let p = { name: 'weirui', age: 18 };
        function saveDate() {
            // key-value   键值对存储，都是字符串  String类型
            localStorage.setItem('msg', 'hello');
            // JSON.stringify()   json 将对象转换为字符串
            localStorage.setItem('person', JSON.stringify(p));
        }

        function readDate() {
            console.log(localStorage.getItem('msg'));

            let res = localStorage.getItem('person');
            console.log(JSON.parse(res));
        }

        function deletDate() {
            localStorage.removeItem('msg');
        }

        function deletAllDate() {
            localStorage.clear()
        }

    </script>
</body>

</html>